package com.mmp.main.servlet.report;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.JobOrder;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;

public class ViewDevicesUnderMaintenance extends BasicServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		int siteId = getSessionSiteId(request);
		int locationId = getSessionLocationId(request);

		String jpql = "SELECT j FROM JobOrder j WHERE j.hospitalDevice.site.id = ? AND j.cancelled = false AND j.closed = false AND j.hospitalDevice.status NOT IN (?,?)";

		Map<Integer, Object> map = new HashMap<Integer, Object>();
		map.put(1, siteId);
		map.put(2, Enums.DEVICE_STATUS.SCRAPPED.getStatus());
		map.put(3, Enums.DEVICE_STATUS.TRANSFERRED.getStatus());

		if (locationId != 0) {
			jpql += " AND j.hospitalDevice.hospital.id = ?";
			map.put(4, locationId);
		}

		jpql += " ORDER BY j.hospitalDevice.hospital.id, j.hospitalDevice.hospitalDepartment.name, j.jobOrderDate";

		List<JobOrder> jobOrderList = dbQueryManager.queryJPQL(jpql, map, JobOrder.class);
		if (jobOrderList == null || jobOrderList.isEmpty()) {
			announceErorrMessage(request, response, "لا يوجد أجهزة تحت الصيانة");
			return;
		}

		HashMap params = new HashMap();
		params.put(Default.REPORT_TITLE_PARAM_NAME, Enums.REPORT_TITLE.DEVICES_UNDER_MAINTENANCE.getReportTitle());
		params.put("jobOrderList", jobOrderList);
		viewPDFReport("rt2.jasper", null, params, request, response);

	}
}
